package com.hmy.finance.service.core.dto;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hmy.finance.api.bo.core.request.QueryDeviceRentalRecordRemainingPauseDayRequest;
import com.hmy.finance.api.bo.core.request.enums.FeeTypeEnum;
import com.hmy.finance.api.bo.core.response.QueryDeviceRentalRecordRemainingPauseDayResponse;
import com.hmy.finance.common.enums.ProfitLossStatusEnum;
import com.hmy.finance.dal.po.DeviceProfitLossDetailPo;
import com.hmy.finance.dal.po.DeviceRentalRecordPo;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 查询设备租赁记录剩余暂停天数请求
 *
 * @author songkf
 */
@Data
public class QueryDeviceRentalRecordRemainingPauseDayDto implements Serializable {

    private static final long serialVersionUID = 7176870737594948968L;
    /**
     * 合同ID
     */
    private Long contractId;
    /**
     * 进场单据ID
     */
    private Long entryFormId;
    /**
     * 设备ID
     */
    private Long deviceId;

    //========== Logic Process Parameter ==========
    /**
     * 设备租赁记录
     */
    private DeviceRentalRecordPo deviceRentalRecord;
    /**
     * 设备未生效报停损益记录
     */
    private List<DeviceProfitLossDetailPo> deviceProfitLossDetailPoList;

    /**
     * 组装查询设备租赁记录剩余暂停天数请求
     *
     * @param request 查询设备租赁记录剩余暂停天数请求
     * @return 查询设备租赁记录剩余暂停天数请求
     * @author songkf
     */

    public static QueryDeviceRentalRecordRemainingPauseDayDto buildQueryDeviceRentalRecordRemainingPauseDayDto(QueryDeviceRentalRecordRemainingPauseDayRequest request) {
        QueryDeviceRentalRecordRemainingPauseDayDto dto = new QueryDeviceRentalRecordRemainingPauseDayDto();
        dto.setContractId(request.getContractId());
        dto.setEntryFormId(request.getEntryFormId());
        dto.setDeviceId(request.getDeviceId());
        return dto;
    }

    public Wrapper<DeviceRentalRecordPo> buildDeviceRentalRecordPoLambdaQueryWrapper() {
        return Wrappers.lambdaQuery(DeviceRentalRecordPo.class)
                .eq(DeviceRentalRecordPo::getContractId, contractId)
                .eq(DeviceRentalRecordPo::getEntryFormId, entryFormId)
                .eq(DeviceRentalRecordPo::getDeviceId, deviceId);
    }

    public Wrapper<DeviceProfitLossDetailPo> buildDeviceProfitLossDetailPoLambdaQueryWrapper() {
        return Wrappers.lambdaQuery(DeviceProfitLossDetailPo.class)
                .eq(DeviceProfitLossDetailPo::getContractId, contractId)
                .eq(DeviceProfitLossDetailPo::getDeviceRentalRecordId, deviceRentalRecord.getId())
                .eq(DeviceProfitLossDetailPo::getFeeType, FeeTypeEnum.PAUSE_FEE)
                .eq(DeviceProfitLossDetailPo::getStatus, ProfitLossStatusEnum.WAIT_EFFECTIVE)
                .eq(DeviceProfitLossDetailPo::getIsDeleted, Boolean.FALSE)
                ;
    }

    public QueryDeviceRentalRecordRemainingPauseDayResponse getResponse() {
        QueryDeviceRentalRecordRemainingPauseDayResponse response = new QueryDeviceRentalRecordRemainingPauseDayResponse();
        response.setRemainingPauseDay(deviceProfitLossDetailPoList.stream()
                .map(DeviceProfitLossDetailPo::getFee)
                .reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue());
        return response;
    }
}
